//
//  OpenCVUtil.m
//  TestOpenCV
//
//  Created by CityDo on 2019/8/22.
//  Copyright © 2019 CityDo. All rights reserved.
//
#import <opencv2/opencv.hpp>
//#import <opencv2/imgproc/types_c.h>
#import <opencv2/imgcodecs/ios.h>
#import <Foundation/Foundation.h>

#import "OpenCVUtil.h"





#import <UIKit/UIKit.h>


//
//@interface OpenCVUtil (){
//    
//}
//
//@end

@implementation OpenCVUtil


-(void)saveImageToDocumentsPath:(NSString *) suffix With: (cv::Mat) mat {
    //获取沙盒路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取documents路径
    NSString *documentDir = [paths objectAtIndex:0];
    //添加文件名及后缀
    NSString *savedImagePath=[documentDir stringByAppendingPathComponent:suffix];
    
    cv::imwrite([savedImagePath UTF8String], mat);
}

+ (UIImage*)convertImage:(UIImage*)image {
    cv::Mat cvImage;
    UIImageToMat(image, cvImage);
    
    if(!cvImage.empty()){
        cv::Mat gray;
        // 将图像转换为灰度显示
        cv::cvtColor(cvImage,gray,CV_RGB2GRAY);
        // 应用高斯滤波器去除小的边缘
        cv::GaussianBlur(gray, gray, cv::Size(5,5), 1.2,1.2);
        // 计算与画布边缘
        cv::Mat edges;
        cv::Canny(gray, edges, 0, 50);
        // 使用白色填充
        cvImage.setTo(cv::Scalar::all(225));
        // 修改边缘颜色
        cvImage.setTo(cv::Scalar(0,128,255,255),edges);
        // 将Mat转换为Xcode的UIImageView显示
        return MatToUIImage(cvImage);
    }
    return nil;
}




@end

